Skip to content

Sized Hierarchy: Part I #137944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Conversation

davidtwco
Copy link
Member

@davidtwco davidtwco commented Mar 3, 2025

This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library, MetaSized and PointeeSized. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract.

These traits are unstable (as is their constness), so users cannot refer to them without opting-in to feature(sized_hierarchy). These traits are not behind cfgs as this would make implementation unfeasible, there would simply be too many cfgs required to add the necessary bounds everywhere. So, like Sized, these traits are automatically implemented by the compiler.

RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows:

  • ?Sized is rewritten as MetaSized
  • MetaSized is added as a default supertrait for all traits w/out an explicit sizedness supertrait already.

There are no edition migrations implemented in this, as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing ?Sized even if the compiler sees MetaSized) unless the sized_hierarchy feature is enabled.

Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax Deref::Target (this will be investigated separately).

It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output.

Notes:

  • Any commits starting with "upstream:" can be ignored, as these correspond to other upstream PRs that this is based on which have yet to be merged.
  • This best reviewed commit-by-commit. I've attempted to make the implementation easy to follow and keep similar changes and test output updates together.
    • Each commit has a short description describing its purpose.
    • This patch is large but it's primarily in the test suite.
  • I've worked on the performance of this patch and a few optimisations are implemented so that the performance impact is neutral-to-minor.
  • PointeeSized is a different name from the RFC just to make it more obvious that it is different from std::ptr::Pointee but all the names are yet to be bikeshed anyway.
  • @nikomatsakis has confirmed that this can proceed as an experiment from the t-lang side

Fixes #79409.

r? @ghost (I'll discuss this with relevant teams to find a reviewer)

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-run-make Area: port run-make Makefiles to rmake.rs A-rustdoc-json Area: Rustdoc JSON backend A-rustdoc-search Area: Rustdoc's search feature PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 3, 2025
@rust-log-analyzer

This comment has been minimized.

@davidtwco
Copy link
Member Author

davidtwco commented Mar 3, 2025

I can reproduce this locally but I have no idea why it would be related to this patch. Clippy needed adjusting.

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Mar 3, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@traviscross traviscross added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 4, 2025
@traviscross
Copy link
Contributor

cc @rust-lang/lang

@fee1-dead fee1-dead self-assigned this Mar 4, 2025
@tmiasko
Copy link
Contributor

tmiasko commented Mar 4, 2025

Does this perhaps fix #127336 by rejecting it?

@bors

This comment was marked as resolved.

@davidtwco
Copy link
Member Author

Does this perhaps fix #127336 by rejecting it?

It doesn't currently.

@davidtwco
Copy link
Member Author

Undrafting now that CI passes

davidtwco added 20 commits May 15, 2025 14:10
When `sized_hierarchy` is enabled, rustc should print `MetaSized` or
`PointeeSized` instead of `?Sized` in opaques.
These tests necessarily need to change now that `?Sized` is not
sufficient to accept extern types and `PointeeSized` is now necessary. In
addition, the `size_of_val`/`align_of_val` test can now be changed to
expect an error.
With the addition of new bounds to the unconstrained parameters, there
are more errors which just need blessed.
It seems like generics from `non_lifetime_binders` don't have any default
bounds like normal generics, so all of the `?Sized` relaxations need
to be further relaxed with `PointeeSized` for this test to be the
equivalent of before.
This test no longer crashes the compiler as `Box` no longer accepts
`PointeeSized`-types. It eventually could, but not because of
`Deref::Target` currently, so this doesn't fail anymore and there wasn't
an obvious to add new types to make it continue to fail because `Deref`
is special.
This test case is a reduction from the `hwc` crate on GitHub, following a
crater run. It passes with the next solver but fails on the current
solver due to a known limitation of the current solver. It starts fails
on the current solver with the `sized_hierarchy` changes because `?Sized`
is now a proper bound.
These tests just need blessing, they don't have any interesting behaviour
changes.

Some of these tests have new errors because `LegacyReceiver` cannot be
proven to be implemented now that it is also testing for `MetaSized` -
but this is just a consequence of the other errors in the test.
Extend the fast path for `Sized` traits to include constness and
`MetaSized`.
As a performance optimization, skip elaborating the supertraits of
`Sized`, and if a `MetaSized` obligation is being checked, then look for
a `Sized` predicate in the parameter environment. This makes the
`ParamEnv` smaller which should improve compiler performance as it avoids
all the iteration over the larger `ParamEnv`.
`nominal_obligations` calls `predicates_of` on a `Sized` obligation,
effectively elaborating the trait and making the well-formedness checking
machinery do a bunch of extra work checking a `MetaSized` obligation is
well-formed, but given that both `Sized` and `MetaSized` are built-ins,
if `Sized` is otherwise well-formed, so `MetaSized` will be.
These should never be shown to users at the moment.
Some rustdoc tests are `no_core` and need to have `MetaSized` and
`PointeeSized` added to them.
As before, updating types using extern types to use `PointeeSized`
bounds.
Unexpected Clippy lint triggering is fixed in upcoming commits but
is necessary for `cfg(bootstrap)`.
Existing lints that had special-casing for `Sized` predicates ought
to have these same special cases applied to `MetaSized` predicates.
One clippy test is `no_core` and needs to have `MetaSized` and
`PointeeSized` added to it.
As in many previous commits, adding the new traits to minicore, but this
time for cranelift and gcc.
It isn't clear why the `Deref` impl isn't found for this in a stage two
build, but presumably relates to `rustc_middle::ty::RawList` containing
an extern type and `Deref` not yet being relaxed to `PointeeSized` (this
is technically a breaking change but unlikely to be one and will be
tested in a follow-up).
These error messages include lines of the standard library which have
changed and so need updated.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 16, 2025
…trait-ref, r=lcnr

trait_sel: deep reject `match_normalize_trait_ref`

Spotted during an in-person review of rust-lang#137944 at RustWeek: `match_normalize_trait_ref` could be using `DeepRejectCtxt` to exit early as an optimisation for projection candidates, like is done with param candidates.

r? `@lcnr`
cc `@oli-obk`
@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok! I'm done 😆

🚢 it the moment #140386 lands

Comment on lines -165 to 170

ItemKind::Trait(_, _, _, _, self_bounds, ..)
| ItemKind::TraitAlias(_, _, self_bounds) => {
ItemKind::Trait(_, _, _, _, self_bounds, ..) => {
is_trait = Some((self_bounds, item.span));
}
ItemKind::TraitAlias(_, _, self_bounds) => {
is_trait = Some((self_bounds, item.span));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened here? Rebase fallout? I think it should be reverted

Comment on lines +150 to +162
if ecx.cx().is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::MetaSized)
&& ecx.cx().is_lang_item(trait_clause.def_id(), TraitSolverLangItem::Sized)
{
let meta_sized_clause = trait_clause
.map_bound(|c| TraitPredicate {
trait_ref: TraitRef::new_from_args(
ecx.cx(),
goal.predicate.def_id(),
c.trait_ref.args,
),
polarity: c.polarity,
})
.upcast(ecx.cx());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pull this out into an #[inline(always)] helper function and dedup with below

Comment on lines -68 to +70
if tcx.is_lang_item(def_id, LangItem::Copy) {
debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
match tcx.as_lang_item(def_id) {
Some(LangItem::Copy) => {
debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea I did this one in isolation on master, but it hasn't landed yet: #140386

Since there were specific reviews there, let's land that one first


struct Foo<'a, T: PointeeSized>(*mut &'a (), T);

fn requires_metasized<'a, T: MetaSized>(f: &T) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn requires_metasized<'a, T: MetaSized>(f: &T) {}
fn requires_metasized<'a, T: MetaSized>(f: &'a T) {}

//@ compile-flags: --crate-type=lib
#![feature(trait_alias)]

// Tests that `?Sized` is migrated to `MetaSized` in trait aliases.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this test actually testing that?

Comment on lines +15 to +21
note: required by a bound in `Bound`
--> $DIR/normalizes-to-is-not-productive.rs:8:1
|
LL | / trait Bound {
LL | | fn method();
LL | | }
| |_^ required by this bound in `Bound`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bogus note, Can be resolved in follow up, but open an issue in that case

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 21, 2025
@bors
Copy link
Collaborator

bors commented May 21, 2025

☔ The latest upstream changes (presumably #140386) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type system impl looks good to me now, modulo some final nits.

This is a breaking change so landing it as is needs a T-types FCP https://rust-lang.zulipchat.com/#narrow/channel/144729-t-types/topic/sized.20hierarchy.20.28.23137944.29/near/517823418

Alternatively, make MetaSized trivially hold for all types unless feature(sized_hierarchy) or feature(extern_types) is enabled. This would avoid the breakage and likely also the perf impact.

It's kinda unsound™️ when interacting with other uses of MetaSized, but given that this only ever affects extern types, it should not matter

Some(LangItem::PointeeSized) => {
// Unexpected - `PointeeSized` is the absence of bounds.
has_pointee_sized_bound = true;
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please ICE

ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
SizedTraitKind::Sized => Err(NoSolution),
SizedTraitKind::MetaSized => Ok(ty::Binder::dummy(vec![])),
SizedTraitKind::PointeeSized => unreachable!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when do we want the PointeeSized variant of this enum? I feel like pretty much every use site shouldn't consider handling it

) -> Result<Candidate<I>, NoSolution> {
unreachable!("Sized is never const")
unreachable!("Sized/MetaSized/PointeeSized is never const")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unreachable!("Sized/MetaSized/PointeeSized is never const")
unreachable!("Sized/MetaSized is never const")

) -> Result<Candidate<I>, NoSolution> {
panic!("`Sized` does not have an associated type: {:?}", goal);
panic!("`Sized`/`MetaSized`/`PointeeSized` does not have an associated type: {:?}", goal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
panic!("`Sized`/`MetaSized`/`PointeeSized` does not have an associated type: {:?}", goal);
panic!("Sized/MetaSized does not have an associated type: {:?}", goal);

Comment on lines +554 to +564
&& drcx.args_may_unify(
obligation.predicate.skip_binder().trait_ref.args,
candidate.skip_binder().trait_ref.args,
)
&& expected_self_ty.bound_vars() == found_self_ty.bound_vars()
&& {
let expected_self_ty =
infcx.tcx.instantiate_bound_regions_with_erased(expected_self_ty);
let found_self_ty = infcx.tcx.instantiate_bound_regions_with_erased(found_self_ty);
infcx.can_eq(obligation.param_env, expected_self_ty, found_self_ty)
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& drcx.args_may_unify(
obligation.predicate.skip_binder().trait_ref.args,
candidate.skip_binder().trait_ref.args,
)
&& expected_self_ty.bound_vars() == found_self_ty.bound_vars()
&& {
let expected_self_ty =
infcx.tcx.instantiate_bound_regions_with_erased(expected_self_ty);
let found_self_ty = infcx.tcx.instantiate_bound_regions_with_erased(found_self_ty);
infcx.can_eq(obligation.param_env, expected_self_ty, found_self_ty)
};
&& drcx.args_may_unify(
obligation.predicate.skip_binder().trait_ref.args,
candidate.skip_binder().trait_ref.args,
);

the args_may_unify should be enough here 🤔

style: please change this to use early returns instead

/// To improve performance, sizedness traits are not elaborated and so special-casing is required
/// in the trait solver to find a `Sized` candidate for a `MetaSized` obligation. Returns the
/// predicate to used in the candidate for such a `obligation`, given a `candidate`.
pub(crate) fn unelaborated_sizedness_candidate<'tcx>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn unelaborated_sizedness_candidate<'tcx>(
pub(crate) fn lazily_elaborate_sizedness_candidate<'tcx>(

Comment on lines +118 to +121
// elaborated, so check if a `Sized` obligation is being elaborated to a
// `MetaSized` obligation and emit it. Candidate assembly and confirmation
// are modified to check for the `Sized` subtrait when a `MetaSized` obligation
// is present.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// elaborated, so check if a `Sized` obligation is being elaborated to a
// `MetaSized` obligation and emit it. Candidate assembly and confirmation
// are modified to check for the `Sized` subtrait when a `MetaSized` obligation
// is present.
// elaborated. Candidate assembly and confirmation are modified to lazily
// check for the `Sized` subtrait when a `MetaSized` obligation is present.

emit/omit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, please explicitly match on Filter here even if we treat both variants the same for now and add a FIXME that we need to decide this on a per use basis.

Hmm, alternatively add a separate 2 state enum to the elaborate

enum ElaborateSized {
    Yes,
    No,
}

E.g. the overlap check in coherence uses elaboration and this one should properly elaborate MetaSized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-run-make Area: port run-make Makefiles to rmake.rs A-rustdoc-json Area: Rustdoc JSON backend A-rustdoc-search Area: Rustdoc's search feature A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` A-testsuite Area: The testsuite used to check the correctness of rustc F-autodiff `#![feature(autodiff)]` perf-regression Performance regression. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE with unsizing an extern type